<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item label="创建日期">
              <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择开始时间" class="query-group-cust" v-model="queryParam.createTime_begin"></j-date>
              <span class="query-group-split-cust"></span>
              <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择结束时间" class="query-group-cust" v-model="queryParam.createTime_end"></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="合同编号">
              <a-input placeholder="请输入合同编号" v-model="queryParam.contractCode"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="承租方">
                <a-input placeholder="请输入承租方" v-model="queryParam.ownerName"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="单据状态">
                <j-dict-select-tag placeholder="请选择单据状态" v-model="queryParam.bpmStatus" dictCode="bpmn_status"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus" v-has="'XyParkLeasecontractList:add'">新增</a-button>
      <a-button type="primary" icon="download" v-has="'XyParkLeasecontractList:export'" @click="handleExportXls('新亚园区租赁合同')">导出</a-button>
      <a-upload v-has="'XyParkLeasecontractList:import'" name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-dropdown v-if="selectedRowKeys.length > 0" v-has="'XyParkLeasecontractList:batchDelete'">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="rowSelectionConfig"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <span slot="action" slot-scope="text, record">
          <template v-if="isCanStartBpm(record,'bpmStatus')">
            <a @click="handleEdit(record)" v-has="'XyParkLeasecontractList:edit'">编辑</a>
            <a-divider v-has="'XyParkLeasecontractList:edit'" type="vertical" />
          </template>
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item v-has="'XyParkLeasecontractList:update_admin'">
                <a @click="handleEdit(record)">管理员编辑</a>
              </a-menu-item>
              <template v-if="isCanStartBpm(record,'bpmStatus')">
                <a-menu-item v-has="'XyParkLeasecontractList:startProcess'">
                  <a @click="startProcess(record)">发起流程</a>
                </a-menu-item>
                <a-menu-item v-has="'XyParkLeasecontractList:delete'">
                  <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                    <a>删除</a>
                  </a-popconfirm>
                </a-menu-item>
              </template>
              <a-menu-item v-else>
                <a @click="handlePreviewPic(record)">审批进度</a>
              </a-menu-item>
              <a-menu-item v-has="'XyParkLeasecontractList:order'" v-if="record.bpmStatus === '2'">
                <a @click="handleOrder(record)">生成账单</a>
              </a-menu-item>
              <a-menu-item v-has="'XyParkLeasecontractList:print'" v-if="record.bpmStatus === '2' || record.bpmStatus === '1'">
                <a @click="handlePrint(record)">打印</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
        <!-- <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
                <a-menu-item v-if="record.bpmStatus === '1'">
                <a @click="startProcess(record)">发起流程</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span> -->

      </a-table>
    </div>
    <task-modal type="history_his" ref="processInstPicModal"></task-modal>
    <xy-park-leasecontract-modal ref="modalForm" @ok="modalFormOk"/>
  </a-card>
</template>

<script>

import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import XyParkLeasecontractModal from './modules/XyParkLeasecontractModal'
import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
import JDate from '@/components/jeecg/JDate.vue'
import '@/assets/less/TableExpand.less'
import TaskModal from '@/views/modules/task/modal/TaskModal'
import { isCanStartBpm, getCheckboxOnlProps } from '@/utils/bpmn'
import '@/assets/less/TableExpand.less'
import { getAction } from '@/api/manage'
import { getReport, getFlowinfo } from '@/api/process'
import { openPrint } from '@/utils/bpmn'
import { filterDictTextByCache } from '@/components/dict/JDictSelectUtil'
export default {
  name: 'XyParkLeasecontractList',
  mixins: [JeecgListMixin],
  components: {
    JDictSelectTag,
    JDate,
    XyParkLeasecontractModal,
    TaskModal
  },
  data() {
    return {
      description: '新亚园区租赁合同管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '创建人',
          align: 'center',
          dataIndex: 'createBy'
        },
        {
          title: '创建日期',
          align: 'center',
          dataIndex: 'createTime'
        },
        {
          title: '签订日期',
          align: 'center',
          dataIndex: 'signingDate',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '合同编号',
          align: 'center',
          dataIndex: 'contractCode'
        },
        {
          title: '出租方',
          align: 'center',
          dataIndex: 'item'
        },
        {
          title: '出租方名称',
          align: 'center',
          dataIndex: 'company'
        },
        {
          title: '承租方编号',
          align: 'center',
          dataIndex: 'ownerCode'
        },
        {
          title: '承租方',
          align: 'center',
          dataIndex: 'ownerName'
        },
        {
          title: '项目编号',
          align: 'center',
          dataIndex: 'projectCode'
        },
        {
          title: '项目名称',
          align: 'center',
          dataIndex: 'projectName'
        },
        {
          title: '租赁面积（㎡）',
          align: 'center',
          dataIndex: 'leaseArea'
        },
        {
          title: '缴纳方式',
          align: 'center',
          dataIndex: 'rentMethod_dictText'
        },
        {
          title: '租赁结束时间',
          align: 'center',
          dataIndex: 'leaseEndtime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '单据状态',
          align: 'center',
          dataIndex: 'bpmStatus',
          customRender: function(text) {
            return filterDictTextByCache('bpmn_status', text)
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/community/xyParkLeasecontract/list',
        // list: '/online/cgform/api/getData/21ce427fdeeb48c9b812a81c1bec4b82',
        delete: '/community/xyParkLeasecontract/delete',
        deleteBatch: '/community/xyParkLeasecontract/deleteBatch',
        exportXlsUrl: '/community/xyParkLeasecontract/exportXls',
        importExcelUrl: '/community/xyParkLeasecontract/importExcel',
        startProcess: '/activiti/instance/startOnlInstance'
      },
      code: '21ce427fdeeb48c9b812a81c1bec4b82',
      reportCode: '',
      dictOptions: {}
    }
  },
  created() {
    this.loadReport()
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
    rowSelectionConfig: function() {
      return {
        // fixed:true,
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange,
        getCheckboxProps: (record) => getCheckboxOnlProps(record, 'bpmStatus')
      }
    }
  },
  methods: {
    isCanStartBpm,
    startProcess(record) {
      this.$confirm({
        title: '提示',
        content: '确认提交流程吗?',
        onOk: () => {
          var param = {
            tableId: this.code,
            formDataId: record.id,
            formUrl: 'modules/bpm/task/form/OnlineFormDetail',
            formUrlMobile: 'online/OnlineDetailForm'
          }
          getAction(this.url.startProcess, param).then((res) => {
            if (res.success) {
              this.$message.success(res.message)
              this.loadData()
              this.onClearSelected()
            } else {
              this.$message.warning(res.message)
            }
          })
        }
      })
    },
    handlePreviewPic: function(record) {
      var dataId = record.id
      this.$refs.processInstPicModal.preview(this.code, dataId)
    },
    handlePrint(data) {
      if (data.bpmStatus === '2') {
        getFlowinfo({
          dataId: data.id,
          tableId: this.code
        }).then(res => {
          if (res.success && res.result) {
            const data = res.result
            data.reportCode = this.reportCode
            openPrint(data)
          } else {
            this.$message.warning('未查询到流程数据')
          }
        })
      } else {
        const token = this.$store.getters['token']
        const url = window._CONFIG['domianURL'] + '/jmreport/view/639232381560655872?token=' + token + '&contract_code=' + data.contractCode
        window.open(url)
      }
    },
    loadReport() { // 请求流程打印code
      if (!this.code) {
        return false
      }
      getReport({ tableId: this.code }).then(res => {
        if (res.success) {
          this.reportCode = res.result
        }
      })
    },
    handleOrder(row) {
      getAction('/custom/generateBill', {
        formDataId: row.id,
        mainTable: 'xy_park_leasecontract',
        houseTable: 'xy_park_leasecontract_detail',
        costTable: 'xy_park_leasecontract_payinfo'
      }).then(res => {
        if (res.success) {
          this.$message.success('生成账单成功')
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
